package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.util.MinioUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("product/file")
public class FileController {
    @Resource
    private MinioUtil minioUtil;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R upload(MultipartFile file){
        Map<String, String> data = new HashMap<>();
        String url =  minioUtil.upload(file);
        data.put("url", url);
        return R.ok().put("data", data);
    }

    @PostMapping("/uploadBatch")
    public String upload(MultipartFile[] files){
        return minioUtil.uploadBatch(files);
    }

    /**
     * 下载文件
     * @param filename
     */
    @GetMapping("/download/{filename}")
    public void download(@PathVariable String filename, HttpServletResponse res){
        minioUtil.download(filename, res);
    }
}
